/*
 * Config.cpp
 *
 *  Created on: Sep 9, 2011
 *      Author: gassa
 */

#include "../include/config.h"
#include "../include/CUtilities.h"

bool config::instanceFlag = false;
config* config::configInstance = NULL;
string config::path="";
void config::initiateMap(){
	ifstream configStream;
	configStream.open(path.c_str(), ios::in);
	if( configStream.is_open() ){
		string line;
		while( configStream.good() ){
			getline(configStream, line);
			CUtilities::trim(line);
			if(line.size()>0){
			vector<string> v;
			CUtilities::tokenize(line, v, "=");
			if( v.size()==2 ){
				CUtilities::trim(v.at(0));
				CUtilities::trim(v.at(1));
				configMap[v.at(0)] = v.at(1);
			}else{
				cout << "invalid config format" << endl;
			}
}
		}
	configStream.close();
	}else{
		cout << path << " fail to open config file!!" << endl;
		exit(0);
	}
	configStream.open(configMap["stopwords_path"].c_str(), ios::in);
	if( configStream.is_open() ){
		while( configStream.good() ){
			string temp;
			getline(configStream, temp, ',');
			stopwordsString += temp + ",";
			stopWordsList.insert(make_pair(temp, temp));
		}
		configStream.close();
	}else{
		cout << "fail to open stopwords file!!!"<<endl;
	}
}
config* config::getInstance()
{
    if(!instanceFlag){
    	configInstance = new config();
        return configInstance;
    }
        return configInstance;
}

config::~config() {
	// TODO Auto-generated destructor stub
    instanceFlag = false;
    delete configInstance;
}

string config::getValue(string key){
	return configMap[key];
}

map<string,string> config::getStopWordsList(){
	return stopWordsList;
}

string config::getStopWordsString(){
	return stopwordsString;
}
